home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / python2.4 / site.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-04-29  |  13.7 KB  |  473 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. """Append module search paths for third-party packages to sys.path.
  5.  
  6. ****************************************************************
  7. * This module is automatically imported during initialization. *
  8. ****************************************************************
  9.  
  10. In earlier versions of Python (up to 1.5a3), scripts or modules that
  11. needed to use site-specific modules would place ``import site''
  12. somewhere near the top of their code.  Because of the automatic
  13. import, this is no longer necessary (but code that does it still
  14. works).
  15.  
  16. This will append site-specific paths to the module search path.  On
  17. Unix, it starts with sys.prefix and sys.exec_prefix (if different) and
  18. appends lib/python<version>/site-packages as well as lib/site-python.
  19. On other platforms (mainly Mac and Windows), it uses just sys.prefix
  20. (and sys.exec_prefix, if different, but this is unlikely).  The
  21. resulting directories, if they exist, are appended to sys.path, and
  22. also inspected for path configuration files.
  23.  
  24. FOR DEBIAN, this sys.path is augmented with directories in /usr/local.
  25. Local addons go into /usr/local/lib/python<version>/site-packages
  26. (resp. /usr/local/lib/site-python), Debian addons install into
  27. /usr/{lib,share}/python<version>/site-packages.
  28.  
  29. A path configuration file is a file whose name has the form
  30. <package>.pth; its contents are additional directories (one per line)
  31. to be added to sys.path.  Non-existing directories (or
  32. non-directories) are never added to sys.path; no directory is added to
  33. sys.path more than once.  Blank lines and lines beginning with
  34. '#' are skipped. Lines starting with 'import' are executed.
  35.  
  36. For example, suppose sys.prefix and sys.exec_prefix are set to
  37. /usr/local and there is a directory /usr/local/lib/python2.3/site-packages
  38. with three subdirectories, foo, bar and spam, and two path
  39. configuration files, foo.pth and bar.pth.  Assume foo.pth contains the
  40. following:
  41.  
  42.   # foo package configuration
  43.   foo
  44.   bar
  45.   bletch
  46.  
  47. and bar.pth contains:
  48.  
  49.   # bar package configuration
  50.   bar
  51.  
  52. Then the following directories are added to sys.path, in this order:
  53.  
  54.   /usr/local/lib/python2.3/site-packages/bar
  55.   /usr/local/lib/python2.3/site-packages/foo
  56.  
  57. Note that bletch is omitted because it doesn't exist; bar precedes foo
  58. because bar.pth comes alphabetically before foo.pth; and spam is
  59. omitted because it is not mentioned in either path configuration file.
  60.  
  61. After these path manipulations, an attempt is made to import a module
  62. named sitecustomize, which can perform arbitrary additional
  63. site-specific customizations.  If this import fails with an
  64. ImportError exception, it is silently ignored.
  65.  
  66. """
  67. import sys
  68. import os
  69. import __builtin__
  70.  
  71. def makepath(*paths):
  72.     dir = os.path.abspath(os.path.join(*paths))
  73.     return (dir, os.path.normcase(dir))
  74.  
  75.  
  76. def abs__file__():
  77.     """Set all module' __file__ attribute to an absolute path"""
  78.     for m in sys.modules.values():
  79.         
  80.         try:
  81.             m.__file__ = os.path.abspath(m.__file__)
  82.         continue
  83.         except AttributeError:
  84.             continue
  85.             continue
  86.         
  87.  
  88.     
  89.  
  90.  
  91. def removeduppaths():
  92.     ''' Remove duplicate entries from sys.path along with making them
  93.     absolute'''
  94.     L = []
  95.     known_paths = set()
  96.     for dir in sys.path:
  97.         (dir, dircase) = makepath(dir)
  98.         if dircase not in known_paths:
  99.             L.append(dir)
  100.             known_paths.add(dircase)
  101.             continue
  102.     
  103.     sys.path[:] = L
  104.     return known_paths
  105.  
  106.  
  107. def addbuilddir():
  108.     """Append ./build/lib.<platform> in case we're running in the build dir
  109.     (especially for Guido :-)"""
  110.     get_platform = get_platform
  111.     import distutils.util
  112.     if not sys.pydebug or '_d':
  113.         pass
  114.     s = 'build/lib%s.%s-%.3s' % ('', get_platform(), sys.version)
  115.     s = os.path.join(os.path.dirname(sys.path[-1]), s)
  116.     sys.path.append(s)
  117.  
  118.  
  119. def _init_pathinfo():
  120.     '''Return a set containing all existing directory entries from sys.path'''
  121.     d = set()
  122.     for dir in sys.path:
  123.         
  124.         try:
  125.             if os.path.isdir(dir):
  126.                 (dir, dircase) = makepath(dir)
  127.                 d.add(dircase)
  128.         continue
  129.         except TypeError:
  130.             continue
  131.             continue
  132.         
  133.  
  134.     
  135.     return d
  136.  
  137.  
  138. def addpackage(sitedir, name, known_paths):
  139.     """Add a new path to known_paths by combining sitedir and 'name' or execute
  140.     sitedir if it starts with 'import'"""
  141.     if known_paths is None:
  142.         _init_pathinfo()
  143.         reset = 1
  144.     else:
  145.         reset = 0
  146.     fullname = os.path.join(sitedir, name)
  147.     
  148.     try:
  149.         f = open(fullname, 'rU')
  150.     except IOError:
  151.         return None
  152.  
  153.     
  154.     try:
  155.         for line in f:
  156.             if line.startswith('#'):
  157.                 continue
  158.             
  159.             if line.startswith('import'):
  160.                 exec line
  161.                 continue
  162.             
  163.             line = line.rstrip()
  164.             (dir, dircase) = makepath(sitedir, line)
  165.             if dircase not in known_paths and os.path.exists(dir):
  166.                 sys.path.append(dir)
  167.                 known_paths.add(dircase)
  168.                 continue
  169.     finally:
  170.         f.close()
  171.  
  172.     if reset:
  173.         known_paths = None
  174.     
  175.     return known_paths
  176.  
  177.  
  178. def addsitedir(sitedir, known_paths = None):
  179.     """Add 'sitedir' argument to sys.path if missing and handle .pth files in
  180.     'sitedir'"""
  181.     if known_paths is None:
  182.         known_paths = _init_pathinfo()
  183.         reset = 1
  184.     else:
  185.         reset = 0
  186.     (sitedir, sitedircase) = makepath(sitedir)
  187.     if sitedircase not in known_paths:
  188.         sys.path.append(sitedir)
  189.     
  190.     
  191.     try:
  192.         names = os.listdir(sitedir)
  193.     except os.error:
  194.         return None
  195.  
  196.     names.sort()
  197.     for name in names:
  198.         if name.endswith(os.extsep + 'pth'):
  199.             addpackage(sitedir, name, known_paths)
  200.             continue
  201.     
  202.     if reset:
  203.         known_paths = None
  204.     
  205.     return known_paths
  206.  
  207.  
  208. def addsitepackages(known_paths):
  209.     '''Add site-packages (and possibly site-python) to sys.path'''
  210.     prefixes = [
  211.         os.path.join(sys.prefix, 'local'),
  212.         sys.prefix]
  213.     if sys.exec_prefix != sys.prefix:
  214.         prefixes.append(os.path.join(sys.exec_prefix, 'local'))
  215.     
  216.     for prefix in prefixes:
  217.         if prefix:
  218.             if sys.platform in ('os2emx', 'riscos'):
  219.                 sitedirs = [
  220.                     os.path.join(prefix, 'Lib', 'site-packages')]
  221.             elif os.sep == '/':
  222.                 sitedirs = [
  223.                     os.path.join(prefix, 'lib', 'python' + sys.version[:3], 'site-packages'),
  224.                     os.path.join(prefix, 'lib', 'site-python')]
  225.             else:
  226.                 sitedirs = [
  227.                     prefix,
  228.                     os.path.join(prefix, 'lib', 'site-packages')]
  229.             if sys.platform == 'darwin':
  230.                 if 'Python.framework' in prefix:
  231.                     home = os.environ.get('HOME')
  232.                     if home:
  233.                         sitedirs.append(os.path.join(home, 'Library', 'Python', sys.version[:3], 'site-packages'))
  234.                     
  235.                 
  236.             
  237.             for sitedir in sitedirs:
  238.                 if os.path.isdir(sitedir):
  239.                     addsitedir(sitedir, known_paths)
  240.                     continue
  241.             
  242.     
  243.  
  244.  
  245. def setBEGINLIBPATH():
  246.     '''The OS/2 EMX port has optional extension modules that do double duty
  247.     as DLLs (and must use the .DLL file extension) for other extensions.
  248.     The library search path needs to be amended so these will be found
  249.     during module import.  Use BEGINLIBPATH so that these are at the start
  250.     of the library search path.
  251.  
  252.     '''
  253.     dllpath = os.path.join(sys.prefix, 'Lib', 'lib-dynload')
  254.     libpath = os.environ['BEGINLIBPATH'].split(';')
  255.     if libpath[-1]:
  256.         libpath.append(dllpath)
  257.     else:
  258.         libpath[-1] = dllpath
  259.     os.environ['BEGINLIBPATH'] = ';'.join(libpath)
  260.  
  261.  
  262. def setquit():
  263.     """Define new built-ins 'quit' and 'exit'.
  264.     These are simply strings that display a hint on how to exit.
  265.  
  266.     """
  267.     if os.sep == ':':
  268.         exit = 'Use Cmd-Q to quit.'
  269.     elif os.sep == '\\':
  270.         exit = 'Use Ctrl-Z plus Return to exit.'
  271.     else:
  272.         exit = 'Use Ctrl-D (i.e. EOF) to exit.'
  273.     __builtin__.quit = __builtin__.exit = exit
  274.  
  275.  
  276. class _Printer(object):
  277.     '''interactive prompt objects for printing the license text, a list of
  278.     contributors and the copyright notice.'''
  279.     MAXLINES = 23
  280.     
  281.     def __init__(self, name, data, files = (), dirs = ()):
  282.         self._Printer__name = name
  283.         self._Printer__data = data
  284.         self._Printer__files = files
  285.         self._Printer__dirs = dirs
  286.         self._Printer__lines = None
  287.  
  288.     
  289.     def _Printer__setup(self):
  290.         if self._Printer__lines:
  291.             return None
  292.         
  293.         data = None
  294.         for dir in self._Printer__dirs:
  295.             for filename in self._Printer__files:
  296.                 filename = os.path.join(dir, filename)
  297.                 
  298.                 try:
  299.                     fp = file(filename, 'rU')
  300.                     data = fp.read()
  301.                     fp.close()
  302.                 continue
  303.                 except IOError:
  304.                     continue
  305.                 
  306.  
  307.             
  308.             if data:
  309.                 break
  310.                 continue
  311.             None<EXCEPTION MATCH>IOError
  312.         
  313.         if not data:
  314.             data = self._Printer__data
  315.         
  316.         self._Printer__lines = data.split('\n')
  317.         self._Printer__linecnt = len(self._Printer__lines)
  318.  
  319.     
  320.     def __repr__(self):
  321.         self._Printer__setup()
  322.         if len(self._Printer__lines) <= self.MAXLINES:
  323.             return '\n'.join(self._Printer__lines)
  324.         else:
  325.             return 'Type %s() to see the full %s text' % (self._Printer__name,) * 2
  326.  
  327.     
  328.     def __call__(self):
  329.         self._Printer__setup()
  330.         prompt = 'Hit Return for more, or q (and Return) to quit: '
  331.         lineno = 0
  332.         while None:
  333.             
  334.             try:
  335.                 for i in range(lineno, lineno + self.MAXLINES):
  336.                     print self._Printer__lines[i]
  337.             except IndexError:
  338.                 break
  339.                 continue
  340.  
  341.             lineno += self.MAXLINES
  342.             key = None
  343.             while key is None:
  344.                 key = raw_input(prompt)
  345.                 if key not in ('', 'q'):
  346.                     key = None
  347.                     continue
  348.             if key == 'q':
  349.                 break
  350.                 continue
  351.  
  352.  
  353.  
  354. def setcopyright():
  355.     """Set 'copyright' and 'credits' in __builtin__"""
  356.     __builtin__.copyright = _Printer('copyright', sys.copyright)
  357.     if sys.platform[:4] == 'java':
  358.         __builtin__.credits = _Printer('credits', 'Jython is maintained by the Jython developers (www.jython.org).')
  359.     else:
  360.         __builtin__.credits = _Printer('credits', '    Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands\n    for supporting Python development.  See www.python.org for more information.')
  361.     here = os.path.dirname(os.__file__)
  362.     __builtin__.license = _Printer('license', 'See http://www.python.org/%.3s/license.html' % sys.version, [
  363.         'LICENSE.txt',
  364.         'LICENSE'], [
  365.         os.path.join(here, os.pardir),
  366.         here,
  367.         os.curdir])
  368.  
  369.  
  370. class _Helper(object):
  371.     """Define the built-in 'help'.
  372.     This is a wrapper around pydoc.help (with a twist).
  373.  
  374.     """
  375.     
  376.     def __repr__(self):
  377.         return 'Type help() for interactive help, or help(object) for help about object.'
  378.  
  379.     
  380.     def __call__(self, *args, **kwds):
  381.         import pydoc
  382.         return pydoc.help(*args, **kwds)
  383.  
  384.  
  385.  
  386. def sethelper():
  387.     __builtin__.help = _Helper()
  388.  
  389.  
  390. def aliasmbcs():
  391.     '''On Windows, some default encodings are not provided by Python,
  392.     while they are always available as "mbcs" in each locale. Make
  393.     them usable by aliasing to "mbcs" in such a case.'''
  394.     if sys.platform == 'win32':
  395.         import locale
  396.         import codecs
  397.         enc = locale.getdefaultlocale()[1]
  398.         if enc.startswith('cp'):
  399.             
  400.             try:
  401.                 codecs.lookup(enc)
  402.             except LookupError:
  403.                 import encodings
  404.                 encodings._cache[enc] = encodings._unknown
  405.                 encodings.aliases.aliases[enc] = 'mbcs'
  406.             except:
  407.                 None<EXCEPTION MATCH>LookupError
  408.             
  409.  
  410.         None<EXCEPTION MATCH>LookupError
  411.     
  412.  
  413.  
  414. def setencoding():
  415.     """Set the string encoding used by the Unicode implementation.  The
  416.     default is 'ascii', but if you're willing to experiment, you can
  417.     change this."""
  418.     encoding = 'ascii'
  419.     if encoding != 'ascii':
  420.         sys.setdefaultencoding(encoding)
  421.     
  422.  
  423.  
  424. def execsitecustomize():
  425.     '''Run custom site specific code, if available.'''
  426.     
  427.     try:
  428.         import sitecustomize
  429.     except ImportError:
  430.         pass
  431.  
  432.  
  433.  
  434. def main():
  435.     abs__file__()
  436.     paths_in_sys = removeduppaths()
  437.     if os.name == 'posix' and sys.path and os.path.basename(sys.path[-1]) == 'Modules':
  438.         addbuilddir()
  439.     
  440.     paths_in_sys = addsitepackages(paths_in_sys)
  441.     if sys.platform == 'os2emx':
  442.         setBEGINLIBPATH()
  443.     
  444.     setquit()
  445.     setcopyright()
  446.     sethelper()
  447.     aliasmbcs()
  448.     setencoding()
  449.     execsitecustomize()
  450.     if hasattr(sys, 'setdefaultencoding'):
  451.         del sys.setdefaultencoding
  452.     
  453.     
  454.     try:
  455.         import apport_python_hook
  456.     except ImportError:
  457.         pass
  458.  
  459.     apport_python_hook.install()
  460.  
  461. main()
  462.  
  463. def _test():
  464.     print 'sys.path = ['
  465.     for dir in sys.path:
  466.         print '    %r,' % (dir,)
  467.     
  468.     print ']'
  469.  
  470. if __name__ == '__main__':
  471.     _test()
  472.  
  473.